package doyouknow.group.CnAndZhb.controller;

import com.alibaba.fastjson.JSONObject;
import doyouknow.group.CnAndZhb.entity.*;
import doyouknow.group.CnAndZhb.response.BaseControllerReturn;
import doyouknow.group.CnAndZhb.service.*;
import doyouknow.group.CnAndZhb.utils.DateUtil;
import doyouknow.group.CnAndZhb.utils.SerializedField;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Date;
import java.util.List;

/**
 * (TbGateway)表控制层
 *
 * @author cn
 * @since 2020-11-03 21:05:02
 */
@CrossOrigin
@RestController
@RequestMapping("tbGateway")
public class TbGatewayController extends BaseController{
    /**
     * 服务对象
     */
    @Resource
    private TbGatewayService tbGatewayService;

    @Resource
    private TbHotelgwMapService tbHotelgwMapService;

    @Resource
    private TbXmmapService tbXmmapService;

    @Resource
    private TbSceneGwMapService tbSceneGwMapService;

    @Resource
    private TbScenePointBindService tbScenePointBindService;

    @Resource
    private TbDeviceService tbDeviceService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TbGateway selectOne(Integer id) {
        return this.tbGatewayService.queryById(id);
    }


    @GetMapping("authorize/juRunApi")
    @SerializedField(enable = false)
    public Object authorize(String uuid, String type){

        TbGateway tbGateway = this.tbGatewayService.queryByUuid(uuid);
        TbHotelgwMap tbHotelgwMap = this.tbHotelgwMapService.queryByUuid(uuid);

        if((tbGateway != null) && (tbHotelgwMap != null)){
            if(tbHotelgwMap.getActionUserid() == 19){
                if(type.equals("anHui")){
                    tbGateway.setAppid("kjnhkt929lq97njc8jhjq5");
                    tbGateway.setTvBox(null);
                    tbGateway.setUpdateTime(new Date());
                    tbGatewayService.update(tbGateway);
                    return BaseControllerReturn.create(new HashMap<String,String>(){{
                        put("msg","安徽电信授权成功");
                    }},null);
                }else if(type.equals("jiangSu")){
                    TbXmmap tbXmmap = this.tbXmmapService.queryByGM(tbGateway.getMac());
                    if (tbXmmap != null) {
                        tbGateway.setAppid("kjnhkt929lq97njc8jhjq5");
                        tbGateway.setTvBox(null);
                        tbGateway.setUpdateTime(new Date());
                        tbXmmap.setPlatformId(1);
                        tbXmmap.setUpdateTime(new Date());
                        tbGatewayService.update(tbGateway);
                        tbXmmapService.update(tbXmmap);

                        return BaseControllerReturn.create(new HashMap<String, String>() {{
                            put("msg", "苏州电信授权成功");
                        }}, null);
                    }else{
                        return BaseControllerReturn.create(new HashMap<String, String>() {{
                            put("msg", "小度未绑定或重复绑定");
                        }}, null);
                    }
                }else{
                    return BaseControllerReturn.create(new HashMap<String,String>(){{
                        put("msg","授权类型有误");
                    }},null);
                }
            }else{
                return BaseControllerReturn.create(new HashMap<String,String>(){{
                    put("msg","非指定聚润网关");
                }},null);
            }
        }

        return BaseControllerReturn.create(new HashMap<String,String>(){{
            put("msg","uuid错误");
        }},null);
    }

    @GetMapping("rename")
    @SerializedField(enable = false)
    public Object GatewayRoomRename(String uuid){
        TbGateway tbGateway = this.tbGatewayService.queryByUuid(uuid);
        TbHotelgwMap tbHotelgwMap = this.tbHotelgwMapService.queryByUuid(uuid);
        if(tbGateway != null){
            if(tbHotelgwMap != null){
                if(tbHotelgwMap.getActionUserid() == 19 || tbHotelgwMap.getActionUserid() == 26){
                    List<TbSceneGwMap> tbSceneGwMaps = this.tbSceneGwMapService.queryByGM(tbGateway.getMac());
                    //这里返回的是列表
                    List<TbScenePointBind> tbScenePointBinds = this.tbScenePointBindService.queryByGM(tbGateway.getMac());
                    //这里返回的是列表
                    TbXmmap tbXmmap = this.tbXmmapService.queryByGM(tbGateway.getMac());
                    //已完成
                    List<TbDevice> tbDevices = this.tbDeviceService.queryByGM(tbGateway.getMac());
                    //这里返回的是列表

                    if(tbSceneGwMaps.size() == 0 && tbScenePointBinds.size() == 0 && tbXmmap == null && tbDevices.size() == 0){

                        tbHotelgwMap.setIsDelete(1);
                        tbHotelgwMap.setUpdateTime(new Date());
                        tbHotelgwMapService.update(tbHotelgwMap);
                        return BaseControllerReturn.create(new HashMap<String,String>(){{
                            put("msg","网关解绑成功");
                        }}, null);

                    }else {
                        return BaseControllerReturn.create(new HashMap<String,String>(){{
                            put("msg","请先清除网关");
                        }}, null);
                    }
                }else{

                    return BaseControllerReturn.create(new HashMap<String,String>(){{
                        put("msg","非指定用户网关");
                    }}, null);
                }


            }else{
                return BaseControllerReturn.create(new HashMap<String,String>(){{
                    put("msg","网关未绑定");
                }}, null);
            }
        }else{
            return BaseControllerReturn.create(new HashMap<String,String>(){{
                put("msg","网关不存在");
            }},null);
        }
    }

    @GetMapping("information")
    @SerializedField(enable = false)
    public Object GatewayInformation(String uuid){
        TbGateway tbGateway = this.tbGatewayService.queryByUuid(uuid);
        return BaseControllerReturn.create(tbGateway,null);
    }

}